package rpc.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import rpc.enums.ResultCode;

import java.io.Serializable;

/**
 * 提供者执行完成或出错厚向消费者返回的结果对象
 *
 * @Author wupeijie
 * @Date 2020/11/19 17:04
 */
@Data
@NoArgsConstructor
public class RpcResponse<T> implements Serializable {
    private String requestId;//响应对应的请求号
    private Integer code; //状态码
    private String msg; //消息
    private T data; //数据

    public static <T> RpcResponse<T> success(T data, String requestId) {
        RpcResponse<T> response = new RpcResponse<>();
        response.setRequestId(requestId);
        response.setCode(ResultCode.SUCCESS.getCode());
        response.setData(data);
        return response;
    }

    public static <T> RpcResponse<T> fail(ResultCode code, String requestId) {
        RpcResponse<T> response = new RpcResponse<>();
        response.setRequestId(requestId);
        response.setCode(code.getCode());
        response.setMsg(code.getMsg());
        return response;
    }
}
